//
//  LXEmotionManager.m
//  LXChatBox
//
//  Created by zlwl001 on 2017/3/8.
//  Copyright © 2017年 manman. All rights reserved.
//

#import "DLEmoticonManager.h"

@implementation DLEmoticonManager

static NSArray<DLEmoticonModel *> *_allEmoticonList, *_ggEmoticonList, *_emojiEmoticonList;

+ (UIImage *)ggEmoticonIcon {
    return [UIImage imageNamed:@"DGGUIKit.bundle/ggw_1"];
}

+ (UIImage *)emojitEmoticonIcon {
    return [UIImage imageNamed:@"DGGUIKit.bundle/face106"];
}

+ (NSArray *)getAllEmoticonList {
    if (!_allEmoticonList) {
        NSString *bundlePath = [[NSBundle mainBundle]  pathForResource:@"DGGUIKit" ofType:@"bundle"];
        NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
        NSString *path = [bundle pathForResource:@"emoji.json" ofType:nil];
        NSData *jsonData = [[NSFileManager defaultManager] contentsAtPath:path];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:1 error:nil];
        NSArray *allEmoticonDictList = dict[@"Emoticon"];
        NSMutableArray *emoticonModelList = [NSMutableArray array];
        for (NSDictionary *dict in allEmoticonDictList) {
            DLEmoticonModel *model = [[DLEmoticonModel alloc] initWithDict:dict];
            [emoticonModelList addObject:model];
        }
        _allEmoticonList = [emoticonModelList copy];
    }
    return _allEmoticonList;
}

+ (NSArray *)ggEmoticonList {
    if (!_ggEmoticonList) {
        NSMutableArray *ggEmoticonList = [NSMutableArray array];
        for (DLEmoticonModel *emoticonModel in [self getAllEmoticonList]) {
            if ([emoticonModel.ID containsString:@"ggw"]) {
                [ggEmoticonList addObject:emoticonModel];
            }
        }
        _ggEmoticonList = [ggEmoticonList copy];
    }
    return _ggEmoticonList;
}

+ (NSArray *)emojiEmoticonList {
    if (!_emojiEmoticonList) {
       NSMutableArray *emojiEmoticonList = [NSMutableArray array];
       for (DLEmoticonModel *emoticonModel in [self getAllEmoticonList]) {
           if ([emoticonModel.ID containsString:@"face"]) {
               [emojiEmoticonList addObject:emoticonModel];
           }
       }
       _emojiEmoticonList = [emojiEmoticonList copy];
    }
    return _emojiEmoticonList;
}

+ (NSArray *)gifEmotion {
    return nil;
}

+ (NSMutableAttributedString *)transferMessageString:(NSString *)message
                                                font:(UIFont *)font
{
    NSMutableAttributedString *attributeStr
    = [[NSMutableAttributedString alloc] initWithString:message];
    NSString *regEmj  = @"\\[[a-zA-Z0-9\\/\\u4e00-\\u9fa5]+\\]";// [微笑]、［哭］等自定义表情处理
    NSError *error    = nil;
    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:regEmj options:NSRegularExpressionCaseInsensitive error:&error];
    if (!expression) {
      NSLog(@"%@",error);
        return attributeStr;
    }
    [attributeStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributeStr.length)];
    NSArray *resultArray = [expression matchesInString:message options:NSMatchingWithTransparentBounds range:NSMakeRange(0, message.length)];
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:resultArray.count];
    for (NSTextCheckingResult *match in resultArray) {
        NSRange range    = match.range;
        NSString *subStr = [message substringWithRange:range];
        NSArray<DLEmoticonModel *> *faceArr;
        if([subStr rangeOfString:@"呱呱蛙"].location !=NSNotFound)//_roaldSearchText
        {
            faceArr = [DLEmoticonManager ggEmoticonList];
        }
        else
        {
           faceArr = [DLEmoticonManager emojiEmoticonList];
        }
    
        
        for (DLEmoticonModel *face in faceArr) {
            if ([face.tag isEqualToString:subStr]) {
                NSTextAttachment *attach   = [[NSTextAttachment alloc] init];
                attach.image               = [UIImage imageNamed:face.bundleFile];
                // 位置调整Y值就行
                attach.bounds              = CGRectMake(0, -4, font.lineHeight, font.lineHeight);
                NSAttributedString *imgStr = [NSAttributedString attributedStringWithAttachment:attach];
                NSMutableDictionary *imagDic   = [NSMutableDictionary dictionaryWithCapacity:2];
                [imagDic setObject:imgStr forKey:@"image"];
                [imagDic setObject:[NSValue valueWithRange:range] forKey:@"range"];
                [mutableArray addObject:imagDic];
            }else if ([face.tag isEqualToString:subStr])
            {
                NSTextAttachment *attach   = [[NSTextAttachment alloc] init];
                attach.image               = [UIImage imageNamed:face.bundleFile];
                // 位置调整Y值就行
                attach.bounds              = CGRectMake(0, -4, font.lineHeight, font.lineHeight);
                NSAttributedString *imgStr = [NSAttributedString attributedStringWithAttachment:attach];
                NSMutableDictionary *imagDic   = [NSMutableDictionary dictionaryWithCapacity:2];
                [imagDic setObject:imgStr forKey:@"image"];
                [imagDic setObject:[NSValue valueWithRange:range] forKey:@"range"];
                [mutableArray addObject:imagDic];
            }
        }
    }
    for (int i =(int) mutableArray.count - 1; i >= 0; i --) {
        NSRange range;
        [mutableArray[i][@"range"] getValue:&range];
        [attributeStr replaceCharactersInRange:range withAttributedString:mutableArray[i][@"image"]];
    }
    return attributeStr;
}
+ (NSMutableAttributedString *)transferMessageAttributedString:(NSMutableAttributedString *)message
                                                          font:(UIFont *)font
{
    
    NSMutableAttributedString *attributeStr = message;
    
    NSString *regEmj  = @"\\[[a-zA-Z0-9\\/\\u4e00-\\u9fa5]+\\]";// [微笑]、［哭］等自定义表情处理
    NSError *error    = nil;
    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:regEmj options:NSRegularExpressionCaseInsensitive error:&error];
    if (!expression) {
        NSLog(@"%@",error);
        return attributeStr;
    }
    [attributeStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributeStr.length)];
    NSArray *resultArray = [expression matchesInString:message.string options:NSMatchingWithTransparentBounds range:NSMakeRange(0, message.length)];
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:resultArray.count];
    for (NSTextCheckingResult *match in resultArray) {
        NSRange range    = match.range;
        NSString *subStr = [message.string substringWithRange:range];
        NSArray<DLEmoticonModel *> *faceArr;
        if([subStr rangeOfString:@"呱呱蛙"].location !=NSNotFound)//_roaldSearchText
        {
            faceArr = [DLEmoticonManager ggEmoticonList];
        }
        else
        {
            faceArr = [DLEmoticonManager emojiEmoticonList];
        }
        
        
        for (DLEmoticonModel *face in faceArr) {
            if ([face.tag isEqualToString:subStr]) {
                NSTextAttachment *attach   = [[NSTextAttachment alloc] init];
                attach.image               = [UIImage imageNamed:face.bundleFile];
                // 位置调整Y值就行
                attach.bounds              = CGRectMake(0, -4, font.lineHeight, font.lineHeight);
                NSAttributedString *imgStr = [NSAttributedString attributedStringWithAttachment:attach];
                NSMutableDictionary *imagDic   = [NSMutableDictionary dictionaryWithCapacity:2];
                [imagDic setObject:imgStr forKey:@"image"];
                [imagDic setObject:[NSValue valueWithRange:range] forKey:@"range"];
                [mutableArray addObject:imagDic];
            }else if ([face.tag isEqualToString:subStr])
            {
                NSTextAttachment *attach   = [[NSTextAttachment alloc] init];
                attach.image               = [UIImage imageNamed:face.bundleFile];
                // 位置调整Y值就行
                attach.bounds              = CGRectMake(0, -4, font.lineHeight, font.lineHeight);
                NSAttributedString *imgStr = [NSAttributedString attributedStringWithAttachment:attach];
                NSMutableDictionary *imagDic   = [NSMutableDictionary dictionaryWithCapacity:2];
                [imagDic setObject:imgStr forKey:@"image"];
                [imagDic setObject:[NSValue valueWithRange:range] forKey:@"range"];
                [mutableArray addObject:imagDic];
            }
        }
    }
    for (int i =(int) mutableArray.count - 1; i >= 0; i --) {
        NSRange range;
        [mutableArray[i][@"range"] getValue:&range];
        [attributeStr replaceCharactersInRange:range withAttributedString:mutableArray[i][@"image"]];
    }
    return attributeStr;
}
@end
